package com.briup.pai.common.utils;

import com.briup.pai.common.enums.ResultCodeEnum;
import com.briup.pai.common.exception.BriupAssert;

/**
 * ClassName：SecurityUtil
 *
 * @Author：小鱼
 * @Create：2025-10-29 9:33
 */

//该数据在同一线程的不同方法中共享
public class SecurityUtil {
    //准备一个threadlocal对象
    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    //存储数据
    public static void setUserId(Integer userId){
        //判断userId是否为空
        BriupAssert.requireNotNull(userId, ResultCodeEnum.PARAM_IS_ERROR);
        if(threadLocal.get()== null){
            threadLocal.set(userId);
        }
    }

    //取出数据
    public static Integer getUserId(){
        return threadLocal.get();
    }

    //删除数据
    public static void removeUserId(){
        threadLocal.remove();
    }
}
